﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpcSelfHost.basePage
{
    public enum SortOption
    {
        FileName,
        Extension,
        CreationTime
    }
    public enum OrderOption
    {
        UP = 1,
        Down = -1
    }
    /// <summary>
    /// SortFile 的摘要说明。
    /// </summary>
    public class SortFile : IComparer
    {
        SortOption mso;
        OrderOption odo;
        public SortFile(SortOption so)
        {
            mso = so;
            odo = OrderOption.UP;
        }
        public SortFile(SortOption so, OrderOption oo)
        {
            mso = so;
            odo = oo;
        }

        int IComparer.Compare(object a, object b)
        {
            try
            {
                FileInfo fa = (FileInfo)a;
                FileInfo fb = (FileInfo)b;

                switch (mso)
                {
                    case SortOption.FileName:
                        return ((int)odo) * String.Compare(fa.Name, fb.Name, true);
                    //break; 
                    case SortOption.Extension:
                        return ((int)odo) * String.Compare(Path.GetExtension(fa.Name), Path.GetExtension(fb.Name), true);
                    //break; 
                    case SortOption.CreationTime:
                        return ((int)odo) * DateTime.Compare(fa.LastWriteTime, fb.LastWriteTime);
                    //break; 
                    default:
                        break;
                }
            }
            catch
            {
                return 0;
            }
            return 0;
        }
    }

}
